package com.graduation.vaccine.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.vaccine.exception.GlobalException;
import com.graduation.vaccine.mapper.DepartmentProductionMapper;
import com.graduation.vaccine.mapper.ProductionMapper;
import com.graduation.vaccine.pojo.DepartmentProduction;
import com.graduation.vaccine.pojo.Production;
import com.graduation.vaccine.service.ProductionService;
import com.graduation.vaccine.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoJunYao
 * @since 2022-04-12
 */
@Service
public class ProductionServiceImpl extends ServiceImpl<ProductionMapper, Production> implements ProductionService {

    @Autowired
    private ProductionMapper productionMapper;
    @Autowired
    private DepartmentProductionMapper departmentProductionMapper;

    /**
     * 返回所有疫苗
     * @return
     */
    @Override
    public RespBean getAll() {
        return RespBean.success(productionMapper.getAll());
    }

    /**
     * 根据疫苗id更新疫苗
     * @return
     */
    @Override
    public RespBean updateProductById(Production production) {
        if(productionMapper.updateById(production)<0){
            throw new GlobalException(RespBeanEnum.UPDATE_ERROR);
        }
        return RespBean.success("更新成功");
    }

    @Override
    public RespBean getInfoByProductionIdAndDepartmentcode(VaccineVo vaccineVo) {
        return RespBean.success(productionMapper.getInfoByProductionIdAndDepartmentcode(vaccineVo));
    }

    /**
     * 根据类别id查找得到列表
     * @param catalogId
     * @return
     */
    @Override
    public RespBean getListByCatalogId(String catalogId) {
        return RespBean.success(productionMapper.getListByCatalogId(catalogId));
    }

    @Override
    public RespBean getProductionList() {
        return RespBean.success(productionMapper.getProductionList());
    }

    @Override
    public RespBean getOrgProductionList(String departmentCode) {
        return RespBean.success(productionMapper.getOrgProductionList(departmentCode));
    }

    @Override
    public RespBean addOne(AddProductionVo addProductionVo) {
        if(productionMapper.addOne(addProductionVo)<1){
            throw new GlobalException(RespBeanEnum.INSERT_ERROR);
        }
        return RespBean.success("添加成功");
    }

    @Override
    public RespBean updateOne(UpdateProductionVo updateProductionVo) {
        if(productionMapper.updateOne(updateProductionVo)<1){
            throw new GlobalException(RespBeanEnum.UPDATE_ERROR);
        }
        return RespBean.success("修改成功");
    }

    @Override
    public RespBean addStock(Integer productionId, Integer number, String departmentCode) {
        //首先判断库存表里面是否有这条数据，没有直接添加
        DepartmentProduction departmentProduction=departmentProductionMapper.getTotalByDepartmentCodeAndProductionId(departmentCode,productionId);
        if(null==departmentProduction){
            if(departmentProductionMapper.addOne(departmentCode,productionId,number)<1){
                throw new GlobalException(RespBeanEnum.INSERT_ERROR);
            }
            return RespBean.success("添加成功");
        }
        Integer version=departmentProduction.getVersion();
        //不为空则修改数据
        if(departmentProductionMapper.addStock(productionId,number,departmentCode,version)<1){
            throw new GlobalException(RespBeanEnum.UPDATE_ERROR);
        }
        return RespBean.success("修改成功");
    }
}
